package nl.scalasim.controlactors

import nl.scalasim.actors.traits.{ComposedActor, StoppableActor}
import nl.scalasim._
import nl.scalasim.util.{AddAction, GoAction, ReplayableGameState}

/**
 * The historian keeps track of the current gamestate. 
 *
 * @author Yigal Duppen
 * @since Nov 7, 2010
 */
class Historian extends ComposedActor
        with StoppableActor
        with Logging
{
  private val state = new ReplayableGameState()


  def handleAct: PartialFunction[Any, Unit] = {
    case Act => {
      log.info("Recording Act")
      state.record(GoAction)
      reply(Ack)
    }
  }
  callbacks :+= handleAct

  def handleActorAdded: PartialFunction[Any, Unit] = {
    case ActorAdded(a, p) => {
      // todo: use capabilities

      if (a.descriptor.name != "Citizen") {
        // ignore citizens, or we end up with twice the amount!
        state.record(AddAction(a.descriptor.name, p))
      }
    }
  }
  callbacks :+= handleActorAdded


  def handleSaveGame: PartialFunction[Any, Unit] = {
    case SaveGameState(f) => {
      log.info("Recording history for later generations")
      reply(SaveGameStateResult(ReplayableGameState.store(state, f)))
    }
  }
  callbacks :+= handleSaveGame



}